package com.one.common.utils;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.one.common.R;


/**
 * author: zhulunjun
 * time:   2017/6/29
 * about:  基于原生的Toast封装的Toast
 * 1.可自定义动画
 * 2.可自定义显示时间
 * <p>
 * 2017/07/13 修复7.1.1（25）以后不显示的问题
 */

public class MyToast {

    private Toast mToast;
    private Context mContext;
    private CharSequence showText;
    private boolean isShow = false;

    private TextView mMessageTextView;//显示的文本
    private ImageView ivToast;

    /**
     * 成功
     */
    public static final int SUCCESS = 1;
    /**
     * 失败
     */
    public static final int FAIL = 2;
//    /**
//     * 警告
//     */
//    public static final int INFO = 3;
//    /**
//     * 出错
//     */
//    public static final int CRY = 4;


    public MyToast(Context mContext) {
        this.mContext = mContext;
        if (mToast == null) {
            mToast = new Toast(mContext);
            initToastView();
        }
    }

    /**
     * 反射调用原生 Toast 的显示方法
     * 并设置隐藏的时间
     */
    public void show() {
        mToast.show();
    }


    /**
     * 初始化Toast的显示视图
     */
    public void initToastView() {

        View view = LayoutInflater.from(mContext).inflate(R.layout.layout_supertoast, null);
        mMessageTextView = (TextView) view.findViewById(R.id.tv_message);
        ivToast = (ImageView) view.findViewById(R.id.iv_toast);
        ivToast.setVisibility(View.GONE);
        mToast.setView(view);
    }

    /**
     * 设置显示文字
     *
     * @param text
     */
    public void setText(CharSequence text) {
        this.showText = text;
        mMessageTextView.setText(showText);
    }

    public CharSequence getText() {
        return showText;
    }

    /**
     * 设置显示位置
     *
     * @param gravity
     * @param xOffset
     * @param yOffset
     */
    public void setGravity(int gravity, int xOffset, int yOffset) {
        if (mToast != null) {
            mToast.setGravity(gravity, xOffset, yOffset);
        }
    }

    /**
     * 设置显示时长
     * 单位：秒
     *
     * @param duration
     */
    public void setDuration(int duration) {
        if (mToast != null) {
            mToast.setDuration(duration);
        }
    }

    public void setBackground(int background) {
        this.mMessageTextView.setBackgroundResource(background);
    }

    /**
     * 设置显示类型
     */
    public void setType(int type) {
        ivToast.setVisibility(View.VISIBLE);
        switch (type) {
            case SUCCESS:
                ivToast.setImageResource(R.mipmap.toast_success);
                break;
            case FAIL:
                ivToast.setImageResource(R.mipmap.toast_fail);
                break;
//            case INFO:
////                ivToast.setImageResource(R.drawable.icon_toast_info);
//                break;
//            case CRY:
////                ivToast.setImageResource(R.drawable.icon_toast_cry);
//                break;
            default:
                ivToast.setVisibility(View.GONE);
                break;

        }
    }

}
